<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col class="box" :title="nodeData.manager">
          {{ nodeData.manager }}
        </el-col>
        <el-col class="box">
          <el-dropdown @command="commandFn">
            <span class="el-dropdown-link" @click.stop>
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!root" command="edit">查看部门</el-dropdown-item>
                <el-dropdown-item v-if="!root" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    root: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    commandFn(command) {
      switch (command) {
        case 'add':
          this.add()
          break
        case 'edit':
          this.edit()
          break
        case 'del':
          this.del()
          break
      }
    },
    add() {
      this.$emit('add', this.nodeData)
    },
    edit() {
      // console.log('edit')
      this.$emit('edit', this.nodeData)
    },
    async del() {
      try {
        await this.$confirm('你确定要删除该部门吗', '温馨提示', { type: 'warning' })
      } catch {
        return
      }
      await delDepartment(this.nodeData.id)
      this.$message.success('删除成功')
      this.$emit('getDepartments')
    }
  }

}
</script>
<style scoped lang="scss">
.box {
  width: 80px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.el-dropdown-link:hover {
  cursor:pointer
}
</style>
